﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hr.IDAL;
using Hr.Model;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.Common.CommandTrees;
using Microsoft.EntityFrameworkCore.Internal;

namespace Hr.DAL
{
    public partial class TaskDAL : BaseDAL<Task_List>, ITask_ListDAL
    {
        //创建EF对象
        private DbContext db = DbContextFactory.Create();
        //DAL方法,核心

        /// <summary>
        /// 获取当前登录用户的任务管理页面信息
        /// </summary>
        /// <param name="id">当前登录的用户id</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/11</create>
        /// <updata>null</updata>

        public IEnumerable<object> SendTaskList(int UserId)
        {

            return (from TaskList in db.Set<Task_List>()
                    join UserDepartment in db.Set<User_Department>() on TaskList.fk_Department equals UserDepartment.pk_Department
                    join UserList in db.Set<User_List>() on TaskList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on TaskList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on TaskList.StatusID equals SysStatusID.ID
                    where TaskList.fk_User == UserId && SysStatusID.TableName.Equals("Task_List") && SysTypeID.TableName.Equals("Task_List")
                    select new
                    {
                        TaskList.pk_Task,
                        TaskList.TypeID,
                        SysTypeID.TypeName_CHS,
                        TaskList.Title,
                        TaskList.CompleteDate,
                        UserList.FullName,
                        UserDepartment.DepartmentName,
                        TaskList.StatusID,
                        SysStatusID.Visual,
                        SysStatusID.StatusName_CHS,
                        TaskList.FilePath
                    }
                   ).ToList();
        }

        /// <summary>
        /// 根据UserList查询用户姓名FullName
        /// </summary>
        /// <param name="id">FullName</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/13</create>
        /// <updata>null</updata>

        public string ReceiveUserTextBinding(string UserList)
        {
            IEnumerable<int> user = UserList.Split('|').ToArray().Select(n => int.Parse(n));
            string ul = "";
            foreach (int item in user)
            {
                ul += (from u in db.Set<User_List>()
                       where u.pk_User == item
                       select u).SingleOrDefault().FullName.ToString() + ",";
            }
            return ul.Substring(0, ul.Length - 1);
        }

        /// <summary>
        /// 根据pk_Task查询需要修改的数据
        /// </summary>
        /// <param name="id">Task_List</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata>

        public Task_List EditSelectBinding(int pk_Task)
        {

            return db.Set<Task_List>().Where(n => n.pk_Task == pk_Task).FirstOrDefault();
        }

        /// <summary>
        /// 根据pk_Task查询需要修改的数据 查看页面
        /// </summary>
        /// <param name="id">Task_List</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/20</create>
        /// <updata>null</updata>

        public object ViewSelectBinding(int pk_Task)
        {
            return (from TaskList in db.Set<Task_List>()
                    join UserDepartment in db.Set<User_Department>() on TaskList.fk_Department equals UserDepartment.pk_Department
                    join UserList in db.Set<User_List>() on TaskList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on TaskList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on TaskList.StatusID equals SysStatusID.ID
                    where TaskList.pk_Task == pk_Task && SysStatusID.TableName.Equals("Task_List") && SysTypeID.TableName.Equals("Task_List")
                    select new
                    {
                        TaskList.pk_Task,
                        TaskList.TypeID,
                        SysTypeID.TypeName_CHS,
                        TaskList.Title,
                        UserDepartment.DepartmentName,
                        TaskList.StatusID,
                        SysStatusID.StatusName_CHS,
                        TaskList.IsCancel,
                        TaskList.IsTop,
                        UserList.FullName,
                        TaskList.Feedback,
                        TaskList.UserList,
                        TaskList.EndDate,
                        TaskList.Description,
                        TaskList.StartDate
                    }
                   ).FirstOrDefault();
        }

        /// <summary>
        /// 根据Title查询数据
        /// </summary>
        /// <param name="id">Title</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/11</create>
        /// <updata>null</updata> 

        public IEnumerable<object> TitleSelectTaskList(int UserId, string Title)
        {

            return (from TaskList in db.Set<Task_List>()
                    join UserDepartment in db.Set<User_Department>() on TaskList.fk_Department equals UserDepartment.pk_Department
                    join UserList in db.Set<User_List>() on TaskList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on TaskList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on TaskList.StatusID equals SysStatusID.ID
                    where TaskList.fk_User == UserId && TaskList.Title.Contains(Title) && SysStatusID.TableName.Equals("Task_List") && SysTypeID.TableName.Equals("Task_List")
                    select new
                    {
                        TaskList.pk_Task,
                        TaskList.TypeID,
                        SysTypeID.TypeName_CHS,
                        TaskList.Title,
                        TaskList.CompleteDate,
                        UserList.FullName,
                        UserDepartment.DepartmentName,
                        TaskList.StatusID,
                        SysStatusID.Visual,
                        SysStatusID.StatusName_CHS,
                        TaskList.FilePath
                    }
                   ).ToList();
        }

        /// <summary>
        /// 下拉框绑定
        /// </summary>
        /// <param name="id">状态下拉框</param>
        /// <returns>修改和增加页面里的下拉框</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata>

        public IEnumerable<Sys_TypeID> SelectTypeBinding()
        {
            return db.Set<Sys_TypeID>().Where(n => n.TableName == "Task_List").ToList();
        }

        /// <summary>
        /// 下拉框绑定
        /// </summary>
        /// <param name="id">状态下拉框</param>
        /// <returns>修改和增加页面里的下拉框</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata>

        public IEnumerable<Sys_StatusID> SelectStatusBinding()
        {
            return db.Set<Sys_StatusID>().Where(n => n.TableName == "Task_List").ToList();
        }

        /// <summary>
        /// 增加和修改
        /// </summary>
        /// <param name="id">pk_Task</param>
        /// <returns>修改和增加页面</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/10</create>
        /// <updata>null</updata>

        public void EditTaskList(Task_List tl, string type, string CheckDt, int pk_User)
        {
            if (type.Equals("Add"))
            {

                //fk_User
                tl.fk_User = pk_User;
                //fk_Department
                tl.fk_Department = db.Set<User_List>().SingleOrDefault(n => n.pk_User == pk_User).fk_Department;
                tl.ImagePath = "";
                tl.FilePath = "";
                tl.UserList = "";
                tl.DepartmentList = "";
                tl.RoleList = "";
                tl.Url = "";
                tl.IsComplete = false;
                tl.CompleteDate = DateTime.Now;
                tl.CancelDate = DateTime.Now;
                tl.UserList = CheckDt;
                tl.Feedback = "";
                //发布人公司
                tl.fk_Company = 0;
                //发布人
                tl.CreateUser = pk_User;
                tl.CreateDate = DateTime.Now;
                //最后修改人
                tl.ModifyUser = pk_User;
                tl.ModifyDate = DateTime.Now;

                db.Set<Task_List>().Add(tl);


                Task_Receiver Tr = new Task_Receiver();

                Tr.pk_Receiver = tl.pk_Task;
                Tr.fk_Task = tl.pk_Task;
                Tr.fk_User = tl.fk_User;
                Tr.fk_Department = tl.fk_Department;
                Tr.IsRead = false;
                Tr.ReadDate = DateTime.Now;
                Tr.IsReceive = false;
                Tr.ReceiveDate = DateTime.Now;
                Tr.StatusID = tl.StatusID;

                db.Set<Task_Receiver>().Add(Tr);

            }
            else if (type.Equals("Updata"))
            {
                Task_List task = db.Set<Task_List>().SingleOrDefault(n => n.pk_Task == tl.pk_Task);
                task.TypeID = tl.TypeID;
                task.StartDate = tl.StartDate;
                task.Title = tl.Title;
                task.Description = tl.Description;
                task.IsTop = tl.IsTop;
                task.FilePath = tl.FilePath;
                task.StatusID = tl.StatusID;
                task.UserList = CheckDt;
                task.EndDate = tl.EndDate;
                task.Feedback = tl.Feedback;
                task.ModifyUser = pk_User;
                task.ModifyDate = DateTime.Now;

                db.Set<Task_List>().AddOrUpdate(task);
            }
        }
        /// <summary>
        /// 修改保存
        /// </summary>
        /// <param name="id">pk_Task</param>
        /// <returns>查看页面保存</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/21</create>
        /// <updata>null</updata>

        public void TaskViewUpdata(Task_List tl,string sumUserFeedback)
        {
            Task_List task = db.Set<Task_List>().SingleOrDefault(n => n.pk_Task == tl.pk_Task);
            task.StatusID = tl.StatusID;
            task.Feedback += sumUserFeedback;
            task.ModifyDate = DateTime.Now;

            db.Set<Task_List>().AddOrUpdate(task);
        }

        /// <summary>
        /// 查询接收人 查询用户表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>新增接收人</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/11</create>
        /// <updata>null</updata>

        public IEnumerable<object> UserList()
        {
            return (from UserList in db.Set<User_List>()
                    join UserDepartment in db.Set<User_Department>() on UserList.fk_Department equals UserDepartment.pk_Department
                    join RoleName in db.Set<Role_Name>() on UserList.RoleValue equals RoleName.pk_Name
                    select new
                    {
                        UserList.pk_User,
                        UserList.ImagePath1,
                        UserDepartment.DepartmentName,
                        UserList.Address1,
                        UserList.FullName,
                        UserList.UserName,
                        RoleName.RoleName_CHS,
                        UserList.Phone1
                    }
                    ).ToList();
        }

        public IEnumerable<object> UserSelectList(string UserSelect)
        {
            return (from UserList in db.Set<User_List>()
                    join UserDepartment in db.Set<User_Department>() on UserList.fk_Department equals UserDepartment.pk_Department
                    join RoleName in db.Set<Role_Name>() on UserList.RoleValue equals RoleName.pk_Name
                    where UserList.FullName.Contains(UserSelect)
                    select new
                    {
                        UserList.pk_User,
                        UserList.ImagePath1,
                        UserDepartment.DepartmentName,
                        UserList.Address1,
                        UserList.FullName,
                        UserList.UserName,
                        RoleName.RoleName_CHS,
                        UserList.Phone1
                    }
                    ).ToList();
        }

        /// <summary>
        /// 获取当前登录用户接收的任务页面信息
        /// </summary>
        /// <param name="id">当前登录的用户id</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/11</create>
        /// <updata>null</updata>

        public IEnumerable<object> ReceiveTaskList(string UserId)
        {

            return (from TaskList in db.Set<Task_List>()
                    join UserDepartment in db.Set<User_Department>() on TaskList.fk_Department equals UserDepartment.pk_Department
                    join UserList in db.Set<User_List>() on TaskList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on TaskList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on TaskList.StatusID equals SysStatusID.ID
                    where TaskList.UserList.Contains(UserId) && SysStatusID.TableName.Equals("Task_List") && SysTypeID.TableName.Equals("Task_List")
                    select new
                    {
                        TaskList.pk_Task,
                        TaskList.TypeID,
                        SysTypeID.TypeName_CHS,
                        TaskList.Title,
                        TaskList.CompleteDate,
                        UserList.FullName,
                        UserDepartment.DepartmentName,
                        TaskList.StatusID,
                        SysStatusID.Visual,
                        SysStatusID.StatusName_CHS,
                        TaskList.FilePath
                    }
                   ).ToList();
        }

        /// <summary>
        /// 根据Title查询数据
        /// </summary>
        /// <param name="id">Title</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/11</create>
        /// <updata>null</updata> 

        public IEnumerable<object> TitleSelectMyTaskList(string UserId, string Title)
        {

            return (from TaskList in db.Set<Task_List>()
                    join UserDepartment in db.Set<User_Department>() on TaskList.fk_Department equals UserDepartment.pk_Department
                    join UserList in db.Set<User_List>() on TaskList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on TaskList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on TaskList.StatusID equals SysStatusID.ID
                    where TaskList.UserList.Contains(UserId) && TaskList.Title.Contains(Title) && SysStatusID.TableName.Equals("Task_List") && SysTypeID.TableName.Equals("Task_List")
                    select new
                    {
                        TaskList.pk_Task,
                        TaskList.TypeID,
                        SysTypeID.TypeName_CHS,
                        TaskList.Title,
                        TaskList.CompleteDate,
                        UserList.FullName,
                        UserDepartment.DepartmentName,
                        TaskList.StatusID,
                        SysStatusID.Visual,
                        SysStatusID.StatusName_CHS,
                    }
                   ).ToList();
        }

        //实现接口成员
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
